<?php
if ($_SESSION['rank'] < 3) {
    exit(Lang::getBackGlobal('wrong_permissions'));
}
/**
 * @author Martin Pernica
 * @copyright 2008
 */

if (empty($_GET['file'])) {
    require '../libs/dirlist.php';

    function getPluginsFrom($folder, $type)
    {
        $plugins = dirlist('../plugins/' . $folder, $type);
        for ($i = 0; !empty($plugins[$i]); $i++) {
            if ($type == 'files' and eregi('.*\.xml$', $plugins[$i])) {
                $xml = ($folder == 'editors') ? '.xml' : '';
                 if (is_file('..' . DIR_SEP . 'plugins' . DIR_SEP . $folder . DIR_SEP . $plugins[$i])) {

                    $xml = simplexml_load_file('..' . DIR_SEP . 'plugins' . DIR_SEP . $folder .
                        DIR_SEP . $plugins[$i]);
                    $name = (string )$xml->name;
                    $version = (string )$xml->version;

                } else {
                    $name = $plugins[$i];
                    $version = 'unknown';
                }
                $setting = '<a href="index.php?p=plugins&type='.$folder.'&file='.$plugins[$i].'">Upravit</a>';
                echo '<tr onmouseover="this.style.background = \'white\'" onmouseout="this.style.background = \'#f5f5f5\'"><td><strong>' .
                    $name . '</strong></td><td><em>' . $version . '</em></td><td>' . $folder .
                    '</td><td>' . $setting . '</td></tr>';
            } 
        }
    }

    echo '<table cellpadding="5" style="color: black;" class="clean_table">';
    echo '<tr><th>Název</th><th>Verze</th><th>Typ</th><th>Akce</th></tr>';
    getPluginsFrom('content', 'files');
    getPluginsFrom('editors', 'files');
    echo '</table>';
}else{
	if(!empty($_POST)){
        $xml = simplexml_load_file('..' . DIR_SEP . 'plugins' . DIR_SEP . $_GET['type'] . DIR_SEP . $_GET['file']);
        $sxe = new SimpleXMLElement($xml->asXML());
        unset($sxe->params);
        $params = $sxe->addChild('params');
		foreach($_POST as $key => $val){
			$params->addChild($key, $val);
		}
		$sxe->asXML('..' . DIR_SEP . 'plugins' . DIR_SEP . $_GET['type'] . DIR_SEP . $_GET['file']);
	}
    $xml = simplexml_load_file('..' . DIR_SEP . 'plugins' . DIR_SEP . $_GET['type'] . DIR_SEP . $_GET['file']);
	echo '<h2>Nastavení pluginu</h2>';
	echo '<form action="" method="post"><table>';
	foreach((array)$xml->params as $key => $val){
		if($key == 'location'){
            echo '<tr><td>Umístění : </td><td><select name="'.$key.'" size="1">
            <option value="header">Header</options>
            <option value="footer">Footer</options>
			<input type="text" name="'.$key.'" value="'.$val.'" />
			</td></tr>';
		}
		
	}
	echo '<tr><td><input type="submit" value="'.SUBMIT.'" /></td></tr>';
	echo '</table></form>';
}

?>